/*
 * @Author: duanguang
 * @Date: 2021-03-30 16:22:30
 * @LastEditTime: 2021-04-26 14:32:14
 * @LastEditors: duanguang
 * @Description: 
 * @FilePath: /legions-Luban/client/src/home/services/components/tableListViewDomain.tsx
 * 「扫去窗上的尘埃，才可以看到窗外的美景。」
 */
import React from 'react';
import { fullProvider,lazyInject } from 'legions-control-container';
import { inject } from 'legions/store'
import ProjectFormPropertyStore,{ ComponentType } from '../../stores/projectFormProperty';
import {
    action,computed
} from 'mobx';
import get from 'lodash/get';
import set from 'lodash/set';
import has from 'lodash/has';
import { shortHash } from 'legions-lunar/object-hash';
import ProjectListPagePropertyStore from '../../stores/projectListPageProperty';
import { MapperEntity } from 'json-mapper-object';
import { LegionsProConditions } from 'legions-pro-design';
import { QueryPropertyContainerVModel } from '../../models/queryPropertyVModel';
import { PropertyQueryFormFields } from '../../models/form/propertyQueryFormFields';
import { OPREATION_VIEW } from '../../constants/consts';
import ProjectStore from '../../stores/projectStore';
import { message } from 'antd';
import {
    AutoQueryVModelCode,
    TABLE_MAPPING_ENTITY_CODE,
    TABLE_RESET_CODE,
    TABLE_SEARCH_CODE,
    TABLE_SEARCH_PARAMS_CODE,
    TABLE_TOKEN_CODE,
    TABLE_TRANFORM_CODE,
    QUERY_SEARCH_CODE,
    QUERY_RESAT_CODE
} from '../../constants/code/tableConfigCode';
const tableAutoQuerystaticMember = {
    parames: {
        label: '搜索条件',
        value:TABLE_SEARCH_PARAMS_CODE,
    },
    onReset: {
        label: '重置',
        value:TABLE_RESET_CODE,
    },
    onSearch: {
        label: '搜索',
        value:TABLE_SEARCH_CODE,
    },
    token: {
        label: 'token',
        value:TABLE_TOKEN_CODE,
    },
    headers: {
        label: 'headers',
        value:"{'HL-Access-Token':''}",
    },
    model: {
        label: 'model',
        value:AutoQueryVModelCode
    },
    transform: {
        label: '数据转换',
        value:TABLE_TRANFORM_CODE
    },
    mappingEntity: {
        label: '服务端数据转换',
        value:TABLE_MAPPING_ENTITY_CODE,
    },
    onQuerySearch: {
        label: '搜索函数',
        value:QUERY_SEARCH_CODE,
    },
    onQueryReset: {
        label: '重置函数',
        value:QUERY_RESAT_CODE,
    }
}
/** table自动托管枚举类映射关系  */
export const tableAutoQueryEnumMapp = {
    1: 'transform',
    2: 'model',
    3: 'headers',
    4: 'token',
    5: 'onSearch',
    6: 'onReset',
    7:'parames',
    8: 'mappingEntity',
    10: 'onQuerySearch',
    11:'onQueryReset',
}
/** 表格列配置数据-填充代码-枚举映射 */
export const TABLE_COLUMNS_ENUM_MAPP = {
    9:'customColumnsRender'
}
/** 列表页属性设置Service */
@fullProvider(TableListViewDomain)
export class TableListViewDomain {
    /** table 自动托管参数常量数据 */
    static tableAutoQuerystaticMember = tableAutoQuerystaticMember;
    @inject(ProjectListPagePropertyStore)
    listPagePropertyApp!: ProjectListPagePropertyStore;

    @inject(ProjectStore)
    projectStoreApp!:ProjectStore
    /** 列表页-搜索条件属性设置单击事件*/
    onQueryPropertyClick(value: { uid: string; name: string }) {
        const QueryPropertyApp = this.listPagePropertyApp.context.QueryPropertyApp;
        const TablePropertyApp = this.listPagePropertyApp.context.TablePropertyApp;
        QueryPropertyApp.queryPropertyModalRef.viewModel.visible = true;
        const item = QueryPropertyApp.queryComponentRef.viewModel.computedQuery.find((item) => item.containerProps.uuid === value.uid);
        const info = QueryPropertyApp.queryTemplate.getQueryComponent(item)
        if (info) {
            QueryPropertyApp.queryPropertyModalRef.viewModel.title = info.title
            //@ts-ignore
            QueryPropertyApp.viewModel.componentActiveType = info.type
            this.listPagePropertyApp.context.QueryPropertyApp.triggerSyncQueryUidEven(value.uid);
            this.projectStoreApp.context.projectCommonApp.modifyActivityComponentUid(value.uid);
        }
        const currComponentView = QueryPropertyApp.queryComponentRef.viewModel.computedQuery.find((item) => item.containerProps.uuid === QueryPropertyApp.editUid)
        if (!(currComponentView instanceof LegionsProConditions.ConditionSearchModel) && !(currComponentView instanceof LegionsProConditions.ConditionGroupCheckBoxModel)) {
            const model = MapperEntity(QueryPropertyContainerVModel,currComponentView);
            const newValue = PropertyQueryFormFields.dataToFormFields<PropertyQueryFormFields,{}>(new PropertyQueryFormFields(),model)
            const timeid = setTimeout(() => {
                QueryPropertyApp.queryConditionsFormProperyRef.store.updateFormInputData(QueryPropertyApp.queryConditionsFormProperyRef.uid,newValue)
                const componentActiveType = QueryPropertyApp.viewModel.componentActiveType;
                if (componentActiveType === 'radioButton' || componentActiveType === 'select') { /** 下拉选择器或者单选组合等类型组件支持添加本地数据 */
                    QueryPropertyApp.queryConditionsFormProperyRef.methods.setFormStates('selectOptionsValueRender',(value) => {
                        value.iFormProps.visible = true;
                    })
                } else {/** 如果首次属性设置是下拉选择器，再次进入则需要检查一次,如果是显示状态，则需要变更为隐藏 */
                    QueryPropertyApp.queryConditionsFormProperyRef.methods.setFormStates('selectOptionsValueRender',(value) => {
                        value.iFormProps.visible = false;
                    })
                }
                if (componentActiveType === 'checkBox') {
                    QueryPropertyApp.queryConditionsFormProperyRef.methods.setFormStates('labelName',(value) => {
                        value.iFormProps.visible = false;
                    })
                }
                QueryPropertyApp.queryConditionsFormProperyRef.viewModel.form.validateFields(null)
                clearTimeout(timeid)
            },100)
        }

    }
    
    /** 单击添加搜索条件视图 */
    onClickAddQuery = (type: string) => {
        this.listPagePropertyApp.context.QueryPropertyApp.insertQuerys(type,this.onQueryPropertyClick.bind(this))
    }
    /** 单击添加操作按钮视图 */
    onOperationAdd = (type: string) => {
        const TbalePropertyApp = this.listPagePropertyApp.context.TablePropertyApp;
        this.projectStoreApp.projectEditViewModel.dispatchAction(() => {
            const defaultView = OPREATION_VIEW[type]
            const _Index = TbalePropertyApp.viewModel.operationConfigCode.findIndex(item => item.type === type);
            if (_Index > -1 && defaultView) {
                message.warning(`${defaultView['name']}不能重复添加`);
                return;
            }
            const nameEn = type.replace(type[0],type[0].toUpperCase());
            const opView = {
                // @ts-ignore
                type,
                name: defaultView ? defaultView['name'] : '操作按钮',
                btnIcon: defaultView ? defaultView['icon'] : '',
                btnType: defaultView ? defaultView['type'] : '',
                nameEn: defaultView ? nameEn : `${nameEn}${TbalePropertyApp.viewModel.operationConfigCode.length}`,
                apiUrl: '',
                params: [],
                id: new Date().getTime().toString(),
                method: '',
                isNeedApi:false,
            }
            // @ts-ignore
            TbalePropertyApp.viewModel.operationConfigCode.push(opView);
        })

    }
}